package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FpFaUserFeedbackDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpFaUserFeedbackService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpFaUserFeedbackController")
@RequestMapping("/api/fpFaUserFeedback")
public class FpFaUserFeedbackController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserFeedbackService fpFaUserFeedbackService;
	
	@RequestMapping("/listFpFaUserFeedback")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserFeedback list begin .......");
			
//			fpFaUserFeedback.setBegin(pager.getStartIndex());
//			fpFaUserFeedback.setPageSize(pager.getPageSize());
			
			int count = fpFaUserFeedbackService.queryListCount(fpFaUserFeedback);
			
			pager.setTotalCount(count);
			
			List<FpFaUserFeedbackDTO> fpFaUserFeedbackList = null;
			
			if(count > 0){
				fpFaUserFeedbackList = fpFaUserFeedbackService.queryList(fpFaUserFeedback);
			}else{
				fpFaUserFeedbackList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserFeedbackList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserFeedback list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/queryFpFaUserFeedback")
	@ResponseBody
	public ResultMap queryFpFaUserFeedback(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserFeedback queryFpFaUserFeedback begin .......");
			
			List<FpFaUserFeedbackDTO> fpFaUserFeedbackList = fpFaUserFeedbackService.queryList(fpFaUserFeedback);
			
			result.setAttribute("list", fpFaUserFeedbackList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserFeedback queryFpFaUserFeedback end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUserFeedback")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserFeedback  begin .......");
			UserDTO user = SessionUtil.getSession().getUser();
			if(StringUtils.isBlank(fpFaUserFeedback.getFpUserPhone())){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			if(!user.getLoginName().equals(fpFaUserFeedback.getFpUserPhone())){
				result.setStatusCode(ResultMap.STATUS_CODE_ONLOGIN);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			DTOdateSetUtil.setCreateInfo(fpFaUserFeedback);
//			DTOdateSetUtil.setUpdateInfo(fpFaUserFeedback);
			int id = fpFaUserFeedbackService.insert(fpFaUserFeedback);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserFeedback list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("add fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUserFeedback")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserFeedbackDTO dto = fpFaUserFeedbackService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUserFeedback  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUserFeedback")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaUserFeedbackService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUserFeedback  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUserFeedback")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserFeedback);
			
			fpFaUserFeedbackService.update(fpFaUserFeedback);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("update fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUserFeedback")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserFeedbackDTO fpFaUserFeedback){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUserFeedback  begin .......");
			
			Integer id = fpFaUserFeedback.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserFeedback);
			
			fpFaUserFeedbackService.updateForce(fpFaUserFeedback);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUserFeedback list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaUserFeedback error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
